package cn.topevery.garden.dal.dao.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.topevery.garden.dal.dao.ProjPaymentDao;
import cn.topevery.garden.entity.dto.ProjMoneyDTO;
import cn.topevery.garden.entity.po.ProjPayment;
import cn.topevery.garden.entity.query.ProjPaymentQuery;
import cn.topevery.garden.entity.vo.ProjPaymentMoneyVO;
import cn.topevery.garden.entity.vo.ProjPaymentVO;
import cn.topevery.garden.entity.vo.ProjPlanIndexVO;
import cn.topevery.garden.entity.vo.ProjSituationRecordMoneyVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 项目支付表
 *
 * @author code-generate
 * @date 2022-11-02
 */
@Repository
public class ProjPaymentDaoImpl extends BaseDaoImpl<ProjPayment> implements ProjPaymentDao {
    @Override
    public <P extends DbCsp> List<ProjPayment> getSearch(P para) {
        return null;
    }

    public SqlQueryMate getListOrPageSqlQueryMate(ProjPaymentQuery query) {
        String table = " t_proj_payment tpp " +
                " inner join t_proj_basic_info tpbi on tpp.c_proj_id = tpbi.c_proj_id " +
                " inner join t_proj_contract tpc on tpp.c_contract_id = tpc.c_contract_id " +
                " inner join t_proj_request_funds_plan tprfp on tpp.c_req_funds_id = tprfp.c_req_funds_id " ;
        String columns = "tpp.*, " +
                " tpbi.c_proj_name," +
                " tpc.c_contract_name," +
                " tprfp.c_req_funds_title ";
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tpp.c_db_create_date asc");
        sqlQueryMate.where(true, "tpp.c_db_status", ScriptConditionEnum.Eq, 0);
        sqlQueryMate.where(true, "tpbi.c_db_status", ScriptConditionEnum.Eq, 0);
        sqlQueryMate.where(ObjectUtil.isNotEmpty(query.getProjId()), "tpp.c_proj_id", ScriptConditionEnum.Eq, query.getProjId());
        sqlQueryMate.where(ObjectUtil.isNotEmpty(query.getYear()), "tpp.c_year", ScriptConditionEnum.Eq, query.getYear());
        sqlQueryMate.where(ObjectUtil.isNotEmpty(query.getContractId()), "tpp.c_contract_id", ScriptConditionEnum.Eq, query.getContractId());
        sqlQueryMate.where(ObjectUtil.isNotEmpty(query.getContractName()), "tpc.c_contract_name", ScriptConditionEnum.Like, query.getContractName());
        sqlQueryMate.where(ObjectUtil.isNotEmpty(query.getIds()), "tpp.c_id", ScriptConditionEnum.In, query.getIds());
        return sqlQueryMate;
    }

    @Override
    public List<ProjPaymentVO> list(ProjPaymentQuery query) {
        SqlQueryMate sqlQueryMate = getListOrPageSqlQueryMate(query);
        return selectList(sqlQueryMate, ProjPaymentVO.class);
    }

    @Override
    public DataPager<ProjPaymentVO> listPage(ProjPaymentQuery query) {
        Integer pageIndex = query.pageIndex - 1;
        SqlQueryMate sqlQueryMate = getListOrPageSqlQueryMate(query);
        return getDataPager(sqlQueryMate, query.pageSize, pageIndex, ProjPaymentVO.class);
    }

    @Override
    public ProjPayment getEntityByGuidId(String projPaymentId) {
        return getEntityByColumnEqValue(new KeyValue<>("c_payment_id", projPaymentId));
    }

    @Override
    public ProjPaymentMoneyVO getPaymentMoney(ProjMoneyDTO moneyDTO) {
        String table = "t_proj_payment tpp";

        String columns = " sum(tpp.c_payment_money) c_payment_money";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tpp.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(ObjectUtil.isNotEmpty(moneyDTO.getProjId()), "tpp.c_proj_id", ScriptConditionEnum.Eq, moneyDTO.getProjId())
                .where(ObjectUtil.isNotEmpty(moneyDTO.getReqFundsId()), "tpp.c_req_funds_id", ScriptConditionEnum.Eq, moneyDTO.getReqFundsId())
                .where(ObjectUtil.isNotEmpty(moneyDTO.getContractId()), "tpp.c_contract_id", ScriptConditionEnum.Eq, moneyDTO.getContractId())
        ;
        return selectOne(sqlQueryMate, ProjPaymentMoneyVO.class);
    }
}
